(provide 'zhang_latex)

;;====使用 LaTeX mode 的时候打开 auto-fill mode 和 outline-minor mode====
(add-hook 'LaTeX-mode-hook
          (function (lambda ()
                      (auto-fill-mode)
                      (LaTeX-math-mode)
                      (imenu-add-menubar-index)
                      (TeX-fold-mode)
                      (outline-minor-mode)
		      ;;(predictive-mode)
		      ))
	  )
(add-hook 'latex-mode-hook
         (function (lambda ()
                      (auto-fill-mode)
                      (LaTeX-math-mode)
                      (imenu-add-menubar-index)
                      (TeX-fold-mode)
                      (outline-minor-mode)
		      ;;(predictive-mode)
		      ))
	 )
;;======================start RefTeX mood================

(require 'reftex)
(add-hook 'LaTeX-mode-hook 'turn-on-reftex)   ; with AUCTeX LaTeX mode
(add-hook 'latex-mode-hook 'turn-on-reftex)   ; with Emacs latex mode
(setq reftex-plug-into-AUCTeX t)
(setq  reftex-enable-partial-scans  t)
(setq  reftex-save-parse-info  t)
(setq  reftex-use-multiple-selection-buffers  t)
(autoload 'reftex-mode "reftex" "RefTeX Minor Mode"  t)
(autoload 'turn-on-reftex  "reftex" "RefTeX Minor Mode"  nil)
(autoload 'reftex-citation "reftex-cite" "Make  citation"  nil)
(autoload 'reftex-index-phrase-mode "reftex-index" "Phrase  mode" t)
;;*toc*buffer  在左侧
(setq  reftex-toc-split-windows-horizontally  t)
;;*toc*buffer  使用整个 frame  的比例。
(setq  reftex-toc-split-windows-fraction  0.2)
(add-hook 'LaTex-mode-hook
	  (function (lambda()
		      define-key LaTeX-mode-map (kbd  "C-c C-p") 'reftex-parse-all)
		    ))

;;===================load cdlatex-mode================
(autoload 'cdlatex-mode "cdlatex" "CDLaTeX Mode" t) 
(autoload 'turn-on-cdlatex "cdlatex" "CDLaTeX Mode" nil)  
(add-hook 'LaTeX-mode-hook 'turn-on-cdlatex) ; with AUCTeX LaTeX mode 
;; CDLaTeX conflicts key bindings [C-c -] with RefTeX. 
;; Because [C-c =] equals to [C-c -] in RefTeX, so I rebind it to CDLaTeX.
(add-hook 'LaTeX-mode-hook 
      (function (lambda () 
              (define-key LaTeX-mode-map 
                 (kbd "C-c -") 'cdlatex-item))))

;;==========AucTex setting===============
(require 'tex-site)
(require 'tex-mik)
(load "auctex.el" nil t t)
(load "preview-latex.el" nil t t)
(setq TeX-electric-escape t)
(setq TeX-auto-save t)
(setq TeX-parse-self t)
(setq global-font-lock-mode t)
(setq-default TeX-master nil)
(setq TeX-PDF-mode t)
(setq TeX-engine (quote xetex))
(setq auto-coding-alist
      (append auto-coding-alist '(("\\.tex\\'" . utf-8)
                  )))
(setq  TeX-fold-env-spec-list
       (quote  ((”[figure]”  (”figure”))
		(”[table]”  (”table”))
		(”[itemize]”(”itemize”))
		(”[overpic]”(”overpic”))
		)))

;;==============set for preview===========================
;;(setq preview-transparent-border 2)
(setq preview-transparent-color '(highlight :background))
(setq preview-scale-function 1.5)
;;==============Load Ebib for manage BibTex database========
(autoload 'ebib "ebib" "Ebib, a BibTeX database manager." t)
(global-set-key "\C-ce" 'ebib)
;;==============use tex font for auctex===================
'(font-latex-fontify-script t)
;;===============setup synctex ===========================
;;(require 'sumatra-forward)
;;(add-hook 'LaTeX-mode-hook (lambda () (local-set-key "\C-x\C-j" 'sumatra-jump-to-line)))
;;(add-hook 'latex-mode-hook (lambda () (local-set-key "\C-x\C-j" 'sumatra-jump-to-line)))
;;(add-hook 'tex-mode-hook (lambda () (local-set-key "\C-x\C-j" 'sumatra-jump-to-line)))
;;'(LaTeX-XeTeX-command "xelatex -synctex=-1")
;;'(TeX-source-correlate-method (quote synctex))
;;'(TeX-source-correlate-mode t)
;;'(TeX-source-correlate-start-server t)
;;(setq TeX-output-view-style (cons '("^pdf$" "." "D:\\GreenTools\\SumatraPDF\\SumatraPDF.exe %o") TeX-output-view-style))
;;================set pdf viewer========================
;;(setq TeX-output-view-style (cons '("^pdf$" "." "start \"D:\\GreenTools\\PDFV_Portable\\PDFXCview.exe\" %o") TeX-output-view-style))
(setq TeX-view-program-list
      '(("SumatraPDF" "D:\\GreenTools\\SumatraPDF\\SumatraPDF.exe %o")
;;        ("Gsview" "gsview32.exe %o")
;;        ("Okular" "okular --unique %o")
;;        ("Evince" "evince %o")
;;        ("Firefox" "firefox %o")
	))
(cond
 ((eq system-type 'windows-nt)
  (add-hook 'LaTeX-mode-hook
            (lambda ()
              (setq TeX-view-program-selection '((output-pdf "SumatraPDF")
                                                 (output-dvi "Yap"))))))
 
 ((eq system-type 'gnu/linux)
  (add-hook 'LaTeX-mode-hook
            (lambda ()
              (setq TeX-view-program-selection '((output-pdf "Okular")
                                                 (output-dvi "Okular")))))))
;;========================================================

;;If one has some citations and references in the document one
;;needs to call `TeX-command´ multiple times. Within the following
;;code the command `TeX-texify´ is defined which runs as often as
;;it is suggested by `TeX-command´.
(require 'tex-buf)
(defun TeX-command-default (name)
  "Next TeX command to use. Most of the code is stolen from `TeX-command-query'."
  (cond ((if (string-equal name TeX-region)
			     (TeX-check-files (concat name "." (TeX-output-extension))
					      (list name)
					      TeX-file-extensions)
			   (TeX-save-document (TeX-master-file)))
			 TeX-command-default)
			((and (memq major-mode '(doctex-mode latex-mode))
			      (TeX-check-files (concat name ".bbl")
					       (mapcar 'car
						       (LaTeX-bibliography-list))
					       BibTeX-file-extensions))
			 ;; We should check for bst files here as well.
			 TeX-command-BibTeX)
			((TeX-process-get-variable name
						   'TeX-command-next
						   TeX-command-Show))
			(TeX-command-Show)))


(defcustom TeX-texify-Show t "Start view-command at end of TeX-texify?" :type 'boolean :group 'TeX-command)
(defcustom TeX-texify-max-runs-same-command 5 "Maximal run number of the same command" :type 'integer :group 'TeX-command)

(defun TeX-texify-sentinel (&optional proc sentinel)
  "Non-interactive! Call the standard-sentinel of the current LaTeX-process.
If there is still something left do do start the next latex-command."
  (set-buffer (process-buffer proc))
  (funcall TeX-texify-sentinel proc sentinel)
  (let ((case-fold-search nil))
    (when (string-match "\\(finished\\|exited\\)" sentinel)
      (set-buffer TeX-command-buffer)
      (unless (plist-get TeX-error-report-switches (intern (TeX-master-file)))
	(TeX-texify)))))

(defun TeX-texify ()
  "Get everything done."
  (interactive)
  (let ((nextCmd (TeX-command-default (TeX-master-file)))
	proc)
    (if (and (null TeX-texify-Show)
	     (equal nextCmd TeX-command-Show))
	(when  (called-interactively-p 'any)
	  (message "TeX-texify: Nothing to be done."))
      (TeX-command nextCmd 'TeX-master-file)
      (when (or (called-interactively-p 'any)
		(null (boundp 'TeX-texify-count-same-command))
		(null (boundp 'TeX-texify-last-command))
		(null (equal nextCmd TeX-texify-last-command)))
	(mapc 'make-local-variable '(TeX-texify-sentinel TeX-texify-count-same-command TeX-texify-last-command))
	(setq TeX-texify-count-same-command 1))
      (if (>= TeX-texify-count-same-command TeX-texify-max-runs-same-command)
	  (message "TeX-texify: Did %S already %d times. Don't want to do it anymore." TeX-texify-last-command TeX-texify-count-same-command)
	(setq TeX-texify-count-same-command (1+ TeX-texify-count-same-command))
	(setq TeX-texify-last-command nextCmd)
	(and (null (equal nextCmd TeX-command-Show))
	     (setq proc (get-buffer-process (current-buffer)))
	     (setq TeX-texify-sentinel (process-sentinel proc))
	     (set-process-sentinel proc 'TeX-texify-sentinel))))))

(add-hook 'LaTeX-mode-hook '(lambda () (local-set-key (kbd "C-c C-a") 'TeX-texify)))


;;===================================================================================
;;(require 'latex-pretty-symbols)
